package org.rabbitmq.rabbitmqspringboot.controller;

import org.apache.tomcat.util.bcel.Const;
import org.rabbitmq.rabbitmqspringboot.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 发送消息
     */
    @RequestMapping("/work")
    public String work(){
        for(int i=0;i<10;i++){
            //使用内置交换机
            //声明队列:
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE,"work queue"+i);
        }
        return "OK";
    }

    @RequestMapping("/fanout")
    public String fanout(){
        //声明队列:
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"","fanout queue");
        return "OK";
    }
    //路由模式
    @RequestMapping("/direct/{routingkey}")
    public String direct(@PathVariable("routingkey") String routingkey){
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,routingkey,"direct queue..");
        return "OK";
    }
    //通配符模式
    @RequestMapping("/topic/{routingkey}")
    public String topic(@PathVariable("routingkey") String routingkey){
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE,routingkey,"topic queue..");
        return "OK";
    }


}
